<template>
  <div>
    <div style="padding-top: 10px; margin-left: 10px">
      <div>
        <el-button
          v-permissions="{ permission: ['add:ExtendSetting'] }"
          type="primary"
          @click="addReception"
        >
          新增
        </el-button>
      </div>
      <el-form
        class="demo-form-inline"
        :inline="true"
        :model="formInline"
        style="margin-top: 10px"
      >
        <el-form-item label="名称">
          <el-input v-model="formInline.name" placeholder="名称" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary">查询</el-button>
        </el-form-item>
      </el-form>

      <div style="margin-top: 10px">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column label="名称" prop="extend_name" />
          <el-table-column label="操作" prop="address">
            <template slot-scope="scope">
              <el-button
                v-permissions="{ permission: ['edit:ExtendSetting'] }"
                type="primary"
                @click="upd(scope.row)"
              >
                修改
              </el-button>
              <el-button
                v-permissions="{ permission: ['delete:ExtendSetting'] }"
                type="primary"
                @click="delet(scope.row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          @current-change="handleCurrentChange"
        /> -->
      </div>
    </div>
    <el-dialog
      :before-close="handleClose"
      title="商品模板"
      :visible.sync="dialogVisible"
      width="70%"
    >
      <span>
        <el-form
          ref="refname"
          class="demo-form-inline"
          :inline="true"
          :model="form"
          style="margin-top: 10px"
        >
          <el-form-item label="名称">
            <el-input v-model="form.extend_name" placeholder="名称" />
          </el-form-item>

          <el-form-item label="分组">
            <el-select v-model="form.type_id" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.type_name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <el-button @click="adviceAdd">添加扩展</el-button>
        <table class="tftable" style="width: 98%">
          <thead>
            <tr>
              <td colspan="1">名称</td>
              <td colspan="1">类型</td>
              <td colspan="1">默认值</td>
              <td colspan="1">输入内容</td>
              <td colspan="1">单位</td>
              <td colspan="1">操作</td>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(items, index) in tableData1" :key="index">
              <td>
                <el-input v-model="items.name" placeholder="请输入内容" />
              </td>
              <td>
                <el-select v-model="items.type" placeholder="请选择">
                  <el-option
                    v-for="item in type"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </td>
              <td>
                <el-input v-model="items.default" placeholder="请输入内容" />
              </td>
              <td>
                <el-input
                  v-if="items.type != 'input_select'"
                  :disabled="true"
                  placeholder="请输入内容"
                />
                <div v-else>
                  <el-tag
                    v-for="(tag, k) in items.option"
                    :key="k"
                    closable
                    :disable-transitions="false"
                    @close="handleCloses(index, k)"
                  >
                    {{ tag }}
                  </el-tag>
                  <el-input
                    v-if="items.inputVisible"
                    ref="saveTagInput"
                    v-model="items.inputValue"
                    class="input-new-tag"
                    size="small"
                    @blur="handleInputConfirm(index)"
                    @keyup.enter.native="handleInputConfirm(index)"
                  />
                  <el-button
                    v-else
                    class="button-new-tag"
                    size="small"
                    @click="showInput(index)"
                  >
                    + New Tag
                  </el-button>
                </div>
              </td>
              <td>
                <el-input v-model="items.unit" placeholder="请输入内容" />
              </td>
              <td>
                <el-button @click.prevent="deleteTest(index)">删除</el-button>
              </td>
            </tr>
          </tbody>
        </table>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getGoodsExtendList,
    addGoodsExtend,
    delGoodsExtend,
  } from '@/api/extend'
  // addGoodsExtend
  // getGoodsExtendList
  import { getRelatedProducts } from '@/api/commodity'

  export default {
    data() {
      return {
        tableData1: [],
        total: 1,
        formInline: {
          name: '',
          pagesize: 10,
          page: 1,
        },
        tableData: [],
        dialogVisible: false,
        options: [],
        form: {
          type_id: '',
          extend_name: '',
        },
        type: [
          {
            name: '文字输入',
            id: 'input_str_w',
          },
          {
            name: '下拉菜单',
            id: 'input_select',
          },
        ],
        dynamicTags: [],
        inputVisible: false,
        inputValue: '',
      }
    },
    mounted() {
      this.init()
    },
    beforeMount() {},
    methods: {
      handleCurrentChange(val) {
        const _this = this
        _this.formInline.page = val
        this.init()
      },
      deleteTest(index) {
        this.tableData1.splice(index, 1)
      },
      upd(e) {
        this.form.type_id = e.type_id
        this.form.extend_name = e.extend_name
        this.form.id = e.id
        const arr = JSON.parse(e.extend_fields)
        arr.forEach((j) => {
          if (j.type != 'input_select') {
            j.option = []
          }
          j.inputVisible = false
          j.inputValue = ''
        })
        this.tableData1 = arr
        this.dialogVisible = true
      },
      delet(r) {
        const data = {
          id: r.id,
        }
        delGoodsExtend(data).then(() => {
          this.init()
        })
      },
      receptonChange() {
        console.log(this.$refs['cascader'])
      },
      addReception() {
        this.form = {
          type_id: '',
          extend_name: '',
        }
        this.tableData1 = []
        this.dialogVisible = true
      },
      init() {
        getRelatedProducts().then((e) => {
          this.options = e.data
        })
        getGoodsExtendList(this.formInline).then((e) => {
          this.tableData = e.data
          // this.total = e.data.total
        })
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(() => {
            done()
          })
          .catch(() => {})
      },
      adviceAdd() {
        this.tableData1.push({
          name: '',
          unit: '',
          type: '',
          default: '',
          option: [],
          inputVisible: false,
          inputValue: '',
        })
      },
      submit() {
        this.tableData1.forEach((e) => {
          if (e.type != 'input_select') {
            //
            delete e.option
          }
          delete e.inputVisible
          delete e.inputValue
        })
        this.form.extend_fields = JSON.stringify(this.tableData1)
        addGoodsExtend(this.form).then(() => {
          this.dialogVisible = false
          this.init()
        })
      },

      handleCloses(i, k) {
        this.tableData1[i].option.splice(k, 1)
      },

      showInput(i) {
        this.tableData1[i].inputVisible = true
        // this.inputVisible = true
      },

      handleInputConfirm(i) {
        console.log(i)
        const add = []
        let inputValue = this.tableData1[i].inputValue
        if (inputValue) {
          console.log(this.tableData1[i].option)
          add.push(inputValue)
          this.tableData1[i].option.push(inputValue)
        }
        // this.inputVisible = false
        this.tableData1[i].inputVisible = false
        this.tableData1[i].inputValue = ''
      },
    },
  }
</script>

<style scoped>
  table.tftable,
  table.table-count {
    font-size: 12px;
    color: #333333;
    width: 100%;
    border-width: 1px;
    border-collapse: collapse;
  }

  table.table-count th,
  table.table-count td {
    font-weight: 400;
    font-style: normal;
    font-size: 12px;
    border-width: 1px;
    border-style: solid;
    border-color: #e4e4e4;
    text-align: center;
  }

  table.tftable th,
  table.tftable td {
    font-weight: 400;
    font-style: normal;
    font-size: 12px;
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #e4e4e4;
    text-align: left;
    text-align: center;
  }

  .tftable {
    margin-left: 1%;
    margin-bottom: 10px;
    margin-top: 10px;
  }
  table.tftable tr:hover {
    background: #c3ecff;
  }
  .tftable > thead > tr:hover {
    background: white;
  }
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
